/**
 * 
 * 广州锐鹏计算机科技有限公司（www.ruipengkj.com）
 * 
 * 
 * Copyright (c) 2011-2012 RUIPENGKJ.Co.Ltd. All rights reserved.
 * 
 * */
package com.ruipengkj.view.baseDataView;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.util.List;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.KeyStroke;

import com.ruipengkj.ApplicationContext;
import com.ruipengkj.bean.TProductCatalog;
import com.ruipengkj.commons.BuildSpringApplication;
import com.ruipengkj.commons.util.MatcherFormatUtil;
import com.ruipengkj.commons.util.UUID;
import com.ruipengkj.commons.util.Util;
import com.ruipengkj.component.JLabelJComboBoxComponent;
import com.ruipengkj.component.JLabelJTextAreaComponent;
import com.ruipengkj.component.JLabelJTextFieldComponent;
import com.ruipengkj.component.RPJButtonWhite;
import com.ruipengkj.component.jdialog.BorderJPanel;
import com.ruipengkj.component.jdialog.IDialog;
import com.ruipengkj.component.jtree.OverwriteDefaultMutableTreeNode;
import com.ruipengkj.service.TProductCatalogService;
/**
 * 说明：
 * 添加商品目录
 * @Author：李智深
 * @Date：2012-03-14
 */
public class DataCommodityTProductCatalogAddDialog extends IDialog {

	private static final long serialVersionUID = 1L;
	
	private JLabelJTextFieldComponent catalogIDTF;//目录编号CHAR(15)PK
	private JLabelJTextFieldComponent preamTF;//父级目录名称
	private JLabelJTextFieldComponent nameTF;//目录名称VARCHAR(256)
	private JLabelJTextAreaComponent desTF;//目录说明VARCHAR(512)
//	private JLabelJTextFieldComponent typeTF;//目录类型TINYINT
	private JLabelJTextFieldComponent unitTF;//商品计量单位VARCHAR(30)目录可以设置默认的计量单位，商品也可以独立设置，一个商品默认的计量单位为该目录的计量单位，如果该目录没有计量单位，则一直往上找，直到找到一个默认计量单位
	private JLabelJComboBoxComponent intoSales;//进销属性-0，标示不设定，1-仅进货，2-仅销售，3-进货销售
	private JLabelJTextFieldComponent priority;
	
	private JButton yesButton; //确定按钮
	private JButton noButton; //取消按钮
	
	private TProductCatalogService tProductCatalogService;
	private OverwriteDefaultMutableTreeNode treeNode;//父级目录节点
	private int nodeRow;//第几行
	
	private DataTProductCatalogSelectDialog dataTProductCatalogSelectDialog;//选择商品类型面板
	
	public boolean isSuccess=false;
	private TProductCatalog tProductCatalog=null;
	
	private boolean isNew = false;
	public DataCommodityTProductCatalogAddDialog(JDialog jd, String title, boolean b) {
		super(jd, title, b);
		this.initViews();
		this.initListener();
		this.setSize(350, 400);
		this.setLocationRelativeTo(null);
	}
	
	private void initViews() {
		tProductCatalogService=BuildSpringApplication.getInstance().getTProductCatalogServiceImpl();
		JPanel main_panel = new BorderJPanel();
		BoxLayout boxLayout = new BoxLayout(main_panel, BoxLayout.Y_AXIS);
		main_panel.setLayout(boxLayout);
		main_panel.setBackground(Util.PANEL_COLOR);
		
		catalogIDTF=new JLabelJTextFieldComponent("  目录编号：",30);//目录编号CHAR(15)PK
		preamTF=new JLabelJTextFieldComponent("<html><font color='red'>*</font> 父级目录：</html>",30);
		nameTF=new JLabelJTextFieldComponent("<html><font color='red'>*</font> 目录名称：</html>",30);//目录名称VARCHAR(256)
//		typeTF=new JLabelJTextFieldComponent(" 目录类型：",30);//目录类型TINYINT
		unitTF=new JLabelJTextFieldComponent("  计量单位：", 30);//计量单位
		desTF=new JLabelJTextAreaComponent("  目录说明：",23,3);//目录说明VARCHAR(512)
		intoSales = new JLabelJComboBoxComponent("  进销类型：", ApplicationContext.INFO_SALES, 188);
		priority= new JLabelJTextFieldComponent("", "类别优先级：","（数字越小，越在前面）", 15);
		main_panel.add(catalogIDTF);
		main_panel.add(preamTF);
		main_panel.add(nameTF);
//		main_panel.add(typeTF);
		main_panel.add(unitTF);
		main_panel.add(intoSales);
		main_panel.add(priority);
		main_panel.add(desTF);
		
		catalogIDTF.getText().setText(UUID.GenUUID15());
//		typeTF.getText().setText(0+"");
		catalogIDTF.getText().setEditable(false);
//		typeTF.getText().setEditable(false);
		JPanel down_jp=uiFactory.createFlowJPanel();
		down_jp.setBackground(Util.PANEL_COLOR);
		yesButton = new RPJButtonWhite("保存(Ctrl+S)");
		noButton = new RPJButtonWhite("取消(Ctrl+Q)");
		down_jp.add(yesButton);
		down_jp.add(noButton);
		main_panel.add(down_jp);
		
		main_panel.setCursor(Util.CURSOR_DEFAULT);
		this.add(main_panel);
	}

	private void initListener(){
		yesButton.addActionListener(this);
		yesButton.registerKeyboardAction(this, KeyStroke.getKeyStroke(KeyEvent.VK_S, KeyEvent.CTRL_MASK), JComponent.WHEN_IN_FOCUSED_WINDOW);
		noButton.addActionListener(this);
		noButton.registerKeyboardAction(this, KeyStroke.getKeyStroke(KeyEvent.VK_Q, KeyEvent.CTRL_MASK), JComponent.WHEN_IN_FOCUSED_WINDOW);
		preamTF.getText().addMouseListener(this);
		preamTF.getText().addKeyListener(this);
	}

	@Override
	protected java.awt.Dimension getTitleSize() {
		return new Dimension(126, 29);
	};
	
	// 修改查看
	public void showDialog(String catalogID, boolean isAdd) {
		if (catalogID != null && !catalogID.equals("")) {
			TProductCatalog tProductCatalog = null;
			try {
				tProductCatalog = tProductCatalogService.getByIdService(catalogID);
			} catch (Exception e1) {
				e1.printStackTrace();
			}
			
			if (isAdd) {
				preamTF.getText().setText(tProductCatalog.getName());
				treeNode = new OverwriteDefaultMutableTreeNode(tProductCatalog.getCatalogID(), tProductCatalog.getName(), tProductCatalog.getDes());
			} else {
				catalogIDTF.getText().setText(tProductCatalog.getCatalogID());
				TProductCatalog preamTProductCatalog = tProductCatalog.getTProductCatalog();
				if (preamTProductCatalog != null && !preamTProductCatalog.getCatalogID().equals("")) {//父目录
					try {
						preamTProductCatalog = tProductCatalogService.getByIdService(preamTProductCatalog.getCatalogID());
						preamTF.getText().setText(preamTProductCatalog.getName());
						treeNode = new OverwriteDefaultMutableTreeNode(preamTProductCatalog.getCatalogID(), preamTProductCatalog.getName(), preamTProductCatalog.getDes());
					} catch (Exception e1) {
						e1.printStackTrace();
					}
				} else {
					treeNode = new OverwriteDefaultMutableTreeNode("", "", "");
					preamTF.getText().setText("所有分类");
				}
//				preamTF.getText().setEnabled(false);
//				preamTF.getText().removeMouseListener(this);
				nameTF.getText().setText(tProductCatalog.getName());
//				typeTF.getText().setText(String.valueOf(tProductCatalog.getType()));
				unitTF.getText().setText(tProductCatalog.getUnit());
				desTF.getArea().setText(tProductCatalog.getDes());
				if (tProductCatalog.getPriority() != null)
					priority.getText().setText(tProductCatalog.getPriority()+"");
				if (tProductCatalog.getIntoSales() != null) {
					intoSales.getBox().setSelectedItem(tProductCatalog.getIntoSales());
				}
			}
		} else if (isAdd) {//选中所有分类添加数据时
			isNew = true;
			treeNode = new OverwriteDefaultMutableTreeNode("", "", "");
			preamTF.getText().setText("所有分类");
//			intoSales.getBox().setSelectedIndex();
		}
		this.setVisible(true);
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource() == yesButton) {
			//确定
			String catalogID=catalogIDTF.getText().getText().trim();
			String pream=preamTF.getText().getText().trim();
			String name=nameTF.getText().getText().trim();
//			String type=typeTF.getText().getText().trim();
			String unit=unitTF.getText().getText().trim();
			String des=desTF.getArea().getText().trim();
			String priorityStr=priority.getText().getText().trim();
			if(name.equals("")||name.length()>256){
				JOptionPane.showMessageDialog(null, "目录名称不能为空，且不能超过256个字");
				return;
			}
			if(pream.equals("")||treeNode==null){
				JOptionPane.showMessageDialog(null, "父级目录不能为空，请重新选择");
				return;
			}
			if (!priorityStr.equals("")) {
				if(!MatcherFormatUtil.MatcherNumberScheme(priorityStr)||priorityStr.length()>10){
					JOptionPane.showMessageDialog(null, "优先级只能输入数字，且不能大于10位");
					priority.getText().requestFocus();
					return;
				}
			} else {
				priorityStr = "0";
			}
			tProductCatalog=new TProductCatalog();
			tProductCatalog.setCatalogID(catalogID);
			if(treeNode.getNodeStringID()==null||treeNode.getNodeStringID().equals("")){
				tProductCatalog.setTProductCatalog(null);//父级目录
			}else{
				tProductCatalog.setTProductCatalog(new TProductCatalog(treeNode.getNodeStringID()));//父级目录
			}
			tProductCatalog.setName(name);
			tProductCatalog.setType((byte)0);//目录类型
			tProductCatalog.setUnit(unit);
			tProductCatalog.setDes(des);
			tProductCatalog.setPriority(Integer.parseInt(priorityStr));
			if (intoSales.getBox().getSelectedIndex() != 0) {
				tProductCatalog.setIntoSales(intoSales.getBox().getSelectedItem().toString());
			}
			try {
//				tProductCatalog.setCatalogID();
				if (isNew) {
					List<TProductCatalog> list = tProductCatalogService.findByNameService(name);
					if (list != null && list.size() != 0) {
						if (Util.showConfirmDialog("存在相同名字的商品类别，确定添加吗？") != 0)
							return;
					}
				}
				tProductCatalogService.saveOrUpdateService(tProductCatalog);
				this.isSuccess=true;
				if (isNew)
					BuildSpringApplication.getInstance().getTOpLogServiceImpl().addOpLogService("添加商品目录信息");
				else
					BuildSpringApplication.getInstance().getTOpLogServiceImpl().addOpLogService("修改商品目录信息");
				JOptionPane.showMessageDialog(null, "添加成功");
			} catch (Exception e1) {
				e1.printStackTrace();
				this.isSuccess=false;
				JOptionPane.showMessageDialog(null, "操作失败");
			}
			this.dispose();
		} else if(e.getSource() == noButton) {
			//取消
			if(Util.showConfirmDialog("是否要取消？")==0){
				this.dispose();
			}
		} else {
			super.actionPerformed(e);
		}
	}
	@Override
	public void mouseClicked(MouseEvent e) {
		if(e.getSource()==preamTF.getText()){
			//选择商品类型
			this.selectProductCatalog();
		}
	}
	@Override
	public void keyPressed(KeyEvent e) {
		if(e.getSource()==preamTF.getText()){
			//选择商品类型
			this.selectProductCatalog();
		}
	}
	private void selectProductCatalog(){
		dataTProductCatalogSelectDialog=new DataTProductCatalogSelectDialog(this, "选择商品类型", true);
		treeNode=dataTProductCatalogSelectDialog.getTreeNode();
		if(treeNode!=null){
			nodeRow=dataTProductCatalogSelectDialog.getNodeRow();
			preamTF.getText().setText(treeNode.getNodeName());
			nameTF.requestFocus();
		}
	}
	
	public OverwriteDefaultMutableTreeNode getTreeNode() {
		return treeNode;
	}

	public TProductCatalog gettProductCatalog() {
		return tProductCatalog;
	}
	
	public int getNodeRow() {
		return nodeRow;
	}
	
	public static void main(String[] args) {
		DataCommodityTProductCatalogAddDialog dialog = new DataCommodityTProductCatalogAddDialog(null, "添加部门", true);
		dialog.setVisible(true);
	}
}